package cn.xd.security.userDetail;

import cn.xd.base.domain.entity.GlobalAppUser;
import cn.xd.common.enums.IBaseEnum;
import cn.xd.common.enums.common.CommonStatusEnum;
import cn.xd.security.userDetail.base.BaseUserDetails;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Collection;


/**
 * 系统用户认证信息
 *
 * @author <a href="mailto:xianrui0365@163.com">haoxr</a>
 * @date 2021/9/27
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class CustomerUserDetails extends BaseUserDetails {
    private String phone;
    private String pwd;
    private CommonStatusEnum status;
    private Collection<SimpleGrantedAuthority> authorities;

    /**
     * 系统管理用户
     */
    public CustomerUserDetails() {
    }


    public static CustomerUserDetails newCustomerUserDetails(GlobalAppUser user) {
        CustomerUserDetails details = new CustomerUserDetails();
        details.setId(user.getId().getValue());
        details.setName(user.getNickName().getValue());
        details.setAvatarUrl(user.getAvatarUrl().getValue());
        details.setGender(user.getGender().getValue());
        details.setPwd(user.getPassword().getValue());
        details.setPhone(user.getPhone().getValue());
        details.setStatus(IBaseEnum.getEnumByCode(user.getStatus().getValue(), CommonStatusEnum.class));
        return details;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.pwd;
    }

    @Override
    public String getUsername() {
        return this.phone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !CommonStatusEnum.DISABLE.equals(status);
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return CommonStatusEnum.ENABLE.equals(status);
    }
}
